<template>
  <el-dialog
    :title="formData.comment"
    :visible="dialogVisible"
    :before-close="handleClose"
    :close-on-click-modal="false"
    width="800px"
    center
  >
    <el-form
      ref="addEditForm"
      v-loading="isLoading > 0"
      label-width="100px"
      class="add-edit-form"
    >
      <el-form-item label="充值规则">
          <el-button type="primary" @click="addRechargeItem()">添加</el-button>
        <ul class="recharge-box">
          <li v-for="(item, i) in rechargeMoneyList" :key="i">
            <span class="label">充值金额（元）：</span>
            <el-input v-model="item.money" class="input"/>
            <span class="label">赠送金额（元）：</span>
            <el-input v-model="item.give" class="input" />
            <span class="label">显示位置：</span>
            <el-input v-model="item.sort" class="input" style="width: 50px;" />
            <i
              class="icon el-icon-remove"
              @click="removeRechargeItem(i)"
            />
          </li>
        </ul>
      </el-form-item>

      <el-form-item class="submit-box">
        <el-button type="primary" @click="submitForm()">提交</el-button>
        <el-button @click="resetForm()">重置</el-button>
      </el-form-item>
    </el-form>
  </el-dialog>
</template>

<script>
import lodash from 'lodash'
import { addOneConfig, updateOneConfig } from '@/api/config'
const emptyFormData = {
  c_value: '',
  status: -1
}
let formData = null
export default {
  name: 'AddEditRechargeMoney',
  components: {
  },
  data() {
    return {
      dialogVisible: false,
      isLoading: 0,
      formData: {},
      rechargeMoneyList: [],
    }
  },
  computed: {},
  watch: {
    'formData.c_value': {
      handler(newVal) {
        if (newVal) {
          this.rechargeMoneyList = JSON.parse(newVal)
        } else {
          return []
        }
      }
    }
  },
  created() {
  },
  mounted() {},
  methods: {
    removeRechargeItem(i) {
      this.rechargeMoneyList.splice(i, 1)
    },
    addRechargeItem() {
      this.rechargeMoneyList.push({ money: null, give: null, sort: 1 })
    },
    handleClose() {
      this.dialogVisible = false
    },
    open(row) {
      this.dialogVisible = true
      if (row) {
        formData = lodash.cloneDeep(row)
      } else {
        formData = lodash.cloneDeep(emptyFormData)
      }
      this.resetForm()
    },
    resetForm() {
      this.formData = lodash.cloneDeep(formData)
      this.$nextTick(() => {
        this.$refs.addEditForm.clearValidate()
      })
    },
  async  submitForm() {
      if (this.isLoading > 0) return
        try {
          this.isLoading++
          this.formData.c_value = JSON.stringify(this.rechargeMoneyList)

          if (this.formData.id) {
            await updateOneConfig(this.formData)
          } else {
            await addOneConfig(this.formData)
          }
          this.$emit('refresh')
          this.handleClose()
        } finally {
          this.isLoading--
        }
    }

  }
}
</script>

<style lang="scss" scoped>
.submit-box {
  display: flex;
  justify-content: flex-end;
}

.recharge-box {
  li {
    display: flex;
    align-items: center;
    margin-top: 10px;
  }
  .input {
    margin-right: 10px;
    width: 100px;
  }
  .label {
    flex: none;
  }
  .icon {
    cursor: pointer;
    font-size: 30px;
    color: #409eff;
  }
}
</style>
